package cn.spring.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.spring.bean.Address;
import cn.spring.bean.Employee;
import cn.spring.service.AddressService;

@Controller
@RequestMapping("/address")
public class AddressController {
	@Autowired
	private AddressService biz;
	
	@RequestMapping("/getAll")
	public String getAll(HttpServletRequest request,Model model){
		Employee emp=(Employee)request.getSession().getAttribute("emp");
		List<Address> list=biz.getAll(emp.getId());
		model.addAttribute("addList", list);
		return "../address";
	}
	
	@RequestMapping("/addAddress")
	public String addAddress(HttpServletRequest request,Model model,@ModelAttribute Address add){
		Employee emp=(Employee)request.getSession().getAttribute("emp");
		add.setEmpId(emp.getId());
		int count=biz.save(add);
		if(count>0){
			List<Address> list=biz.getAll(emp.getId());
			model.addAttribute("addList", list);
			return "../address";
		}else{
			return "../error";
		}
	}
	
	@RequestMapping("/deleteAdd")
	public String deleteAdd(@RequestParam("id")int id,Model model,HttpServletRequest request){
		int count=biz.delete(id);
		if(count>0){
			Employee emp=(Employee)request.getSession().getAttribute("emp");
			List<Address> list=biz.getAll(emp.getId());
			model.addAttribute("addList", list);
			return "../address";
		}else{
			return "../error";
		}
	}
	
	@RequestMapping("/updateMo")
	public String updateIsmoById(@RequestParam("id")int id,@RequestParam("empId")int empId,Model model,HttpServletRequest request) {
		biz.updateIsmoById(id,empId);
		Employee emp=(Employee)request.getSession().getAttribute("emp");
		List<Address> list=biz.getAll(emp.getId());
		model.addAttribute("addList", list);
		return "../address";
	}
	
}
